package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.common.utils.StringUtils;
import com.jiangyao.dao.ICoursewareDao;
import com.jiangyao.dao.ICoursewareDataDao;
import com.jiangyao.model.dto.backend.CoursewareDTO;
import com.jiangyao.model.dto.backend.CoursewareListPageDTO;
import com.jiangyao.model.entity.Courseware;
import com.jiangyao.model.entity.CoursewareData;
import com.jiangyao.model.vo.backend.CoursewareDetailVO;
import com.jiangyao.model.vo.backend.CoursewareListVO;
import com.jiangyao.service.CoursewareService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;

/**
 * <p>
 * 课件信息表 服务实现类
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-11
 */
@Service
public class CoursewareServiceImpl extends BaseServiceImpl<ICoursewareDao, Courseware> implements CoursewareService {

	@Autowired
	private DozerBeanMapper dozerBeanMapper;

	@Autowired
	private ICoursewareDao coursewareDao;

	@Autowired
	private ICoursewareDataDao coursewareDataDao;

	@Override
	public Page<CoursewareListVO> getListPage(CoursewareListPageDTO listPageDTO) {
		Page<CoursewareListVO> page = new Page<>(listPageDTO.getPageNo(), listPageDTO.getPageSize());
		coursewareDao.getListPage(page, listPageDTO);
		return page;
	}

	@Override
	public void add(CoursewareDTO coursewareDTO) {
		Courseware courseware = dozerBeanMapper.map(coursewareDTO, Courseware.class);
		courseware.setCoursewareId(null);
		courseware.setCreateTime(new Date());
		courseware.setCreateAdminId(getLoginAdminUserId());
		courseware.setStatus(1);
		this.save(courseware);
		// 新建课件数据（观看次数等）
		CoursewareData coursewareData = new CoursewareData();
		coursewareData.setCoursewareId(courseware.getCoursewareId());
		coursewareDataDao.insert(coursewareData);
	}

	@Override
	public void edit(CoursewareDTO coursewareDTO) {
		Courseware courseware = this.getById(coursewareDTO.getCoursewareId());
		if (courseware == null) {
			throw new ServerException(100, "课件不存在");
		}
		UpdateWrapper<Courseware> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("courseware_id", coursewareDTO.getCoursewareId());
		updateWrapper.set("category_id", coursewareDTO.getCategoryId());
		updateWrapper.set("courseware_name", coursewareDTO.getCoursewareName());
		updateWrapper.set("courseware_desc", coursewareDTO.getCoursewareDesc());
		updateWrapper.set("resources_path", coursewareDTO.getResourcesPath());
		updateWrapper.set("file_name", coursewareDTO.getFileName());
		updateWrapper.set("total_time", coursewareDTO.getTotalTime());
		updateWrapper.set("update_time", new Date());
		updateWrapper.set("update_admin_id", getLoginAdminUserId());
		this.update(updateWrapper);
	}

	@Override
	public void editStatus(CoursewareDTO coursewareDTO) {
		if (CollectionUtils.isEmpty(coursewareDTO.getCoursewareIdList())) {
			return;
		}
		UpdateWrapper<Courseware> updateWrapper = new UpdateWrapper<>();
		updateWrapper.in("courseware_id", coursewareDTO.getCoursewareIdList());
		updateWrapper.set("status", coursewareDTO.getStatus() == null ? 1 : coursewareDTO.getStatus());
		updateWrapper.set("update_time", new Date());
		updateWrapper.set("update_admin_id", getLoginAdminUserId());
		this.update(updateWrapper);
	}

	@Override
	public CoursewareDetailVO getDetail(CoursewareDTO coursewareDTO) {
		Courseware courseware = this.getById(coursewareDTO.getCoursewareId());
		if (courseware == null) {
			throw new ServerException(100, "课件不存在");
		}
		CoursewareDetailVO coursewareVo = dozerBeanMapper.map(courseware, CoursewareDetailVO.class);
		if (StringUtils.isNotBlank(coursewareVo.getResourcesPath())) {
			coursewareVo.setResourcesPathFull(FtpClient.fullFtpUrl(coursewareVo.getResourcesPath()));
		}
		return coursewareVo;
	}
}
